package com.itbaizhan.dao.impl;

import com.itbaizhan.dao.RedisDao;
import com.itbaizhan.exception.BusException;
import com.itbaizhan.result.CodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * @author spf
 * @date 2024/3/6
 * @time 14:20
 */
@Repository
public class RedisDaoImpl implements RedisDao {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        opsForValue.set(key, value, 1, TimeUnit.MINUTES);
    }

    @Override
    public String get(String key) {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        return (String) opsForValue.get(key);
    }

    @Override
    public Boolean delete(String key) {
        try{
            redisTemplate.delete(key);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw new BusException(CodeEnum.DEL_REDISKEY_ERROR);
        }
    }
}
